package cn.codexin.wechatofficial.controller;

import cn.codexin.wechatofficial.dto.MenuDTO;
import cn.codexin.wechatofficial.service.MenuService;
import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by xinGao 2020/4/23
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @PostMapping(value = "/create", produces = "application/json")
    public String createMenu(HttpServletRequest httpRequest) {
        //获取access_token
        String token = (String) httpRequest.getSession().getAttribute("token");
        //向微信服务器发送建立菜单请求
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.get("application/json; charset=utf-8");
        //菜单对象
        MenuDTO menu = menuService.getMenuDTO();
        String json = JSON.toJSONString(menu);
        System.out.println(json);
        RequestBody body = RequestBody.create(json, mediaType);
        Request request = new Request.Builder()
                .url("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }

    @GetMapping(value = "/get", produces = "application/json")
    public String getMenu(HttpServletRequest httpRequest) {
        // 之后需要添加权限验证

        //获取access_token
        String token = (String) httpRequest.getSession().getAttribute("token");
        //向微信服务器发送建立菜单请求
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.get("application/json; charset=utf-8");
        Request request = new Request.Builder()
                .url("https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + token)
                .get()
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }

    @GetMapping(value = "/del", produces = "application/json")
    public String delMenu(HttpServletRequest httpRequest) {
        // 之后需要添加权限验证

        //获取access_token
        String token = (String) httpRequest.getSession().getAttribute("token");
        //向微信服务器发送建立菜单请求
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.get("application/json; charset=utf-8");
        Request request = new Request.Builder()
                .url("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + token)
                .get()
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }
}
